package com.indexingsystem.boss.utils;

import com.indexingsystem.boss.Vo.ExportTableWithOrderResVo;
import com.indexingsystem.boss.Vo.ExportWordsWithOrderResVo;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ThemeWordsXmlUtil {


    public void exportThemeWords(List<ExportTableWithOrderResVo> dataList, String filePath) throws FileNotFoundException, UnsupportedEncodingException {
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        String dateFilePathStr = df2.format(new Date());
        String dateFileStr = df1.format(new Date());
        String exportPath = null;
        if (filePath.endsWith("\\")) {
            exportPath = filePath + dateFilePathStr;
        } else {
            exportPath = filePath + "\\" + dateFilePathStr;
        }
        File file = new File(exportPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("root");
        Element tables = root.addElement("ThemeTables");
        if (dataList != null && dataList.size() > 0) {
            for (ExportTableWithOrderResVo t : dataList) {
                addThemeTableElement(root, tables, t);
            }
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileOutputStream(exportPath + "\\" + "ThemeWords" + dateFileStr + ".xml"), format);
        try {
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addThemeTableElement(Element top, Element parent, ExportTableWithOrderResVo child) {
        Element element = parent.addElement("ThemeTable");
        element.addAttribute("id", String.valueOf(child.getThemeTableId()));
        element.addAttribute("name", child.getThemeTableName());
        element.addAttribute("code", child.getThemeTableCode());
        element.addAttribute("verify", child.getVerify());
        /*if (child.getOrder().split("\\.").length == 1) {*/
        Element themeWordsElement = top.addElement("ThemeWords");
        themeWordsElement.addAttribute("id", String.valueOf(child.getThemeTableId()));
        themeWordsElement.addAttribute("name", child.getThemeTableName());
        themeWordsElement.addAttribute("code", child.getThemeTableCode());
        List<ExportWordsWithOrderResVo> words = child.getWords();
        if (words != null && words.size() > 0) {
            for (ExportWordsWithOrderResVo w : words) {
                addThemeWordElement(themeWordsElement, w);
            }
        }

        if (child.getTables() != null && child.getTables().size() > 0) {
            for (ExportTableWithOrderResVo t : child.getTables()) {
                addThemeTableElement(top, element, t);
            }
        }
    }

    private void addThemeWordElement(Element parent, ExportWordsWithOrderResVo child) {
        Element element = parent.addElement("ThemeWord");
        element.addAttribute("id", String.valueOf(child.getThemeWordsId()));
        element.addAttribute("name", child.getThemeWordsName());
        element.addAttribute("code", child.getThemeWordsCode());
        element.addAttribute("verify", child.getVerify());
        if (child.getWords() != null && child.getWords().size() > 0) {

            for (ExportWordsWithOrderResVo r : child.getWords()) {
                addThemeWordElement(element, r);
            }
        }
    }

}
